angular.module('mamApp').controller('addContractManagementController', ['$scope', 'ContractManagementManager', '$state', '$uibModal','ImsDialog','$filter',"FileUploader","api",
    function ($scope, ContractManagementManager, $state, $uibModal,ImsDialog,$filter,FileUploader,api) {
    var  cpId=$state.params.cpId;
    $scope.deleteSelect=false;
    $scope.imageMsg_a=false;
    $scope.formData={};
    $scope.chooseCP=function () {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'chooseCP.html',
            controller: 'chooseCPModalController'
        })
        modalInstance.result.then(function (result) {
            $scope.data=result
            $scope.deleteSelect=true;
            $scope.deleteSelectCP =function(){
                $scope.deleteSelect=false;
                $scope.data="";
            }
        })
    }

    $scope.openStart = function(item) {
        item.openedStart=true;
    };
    $scope.rangeDate = {
        startDate:$scope.search.startDay ? moment($scope.search.startDay).format('X'):null,
        endDate:$scope.search.endDay? moment($scope.search.endDay).format('X'):null,
        onSelected: function (data, scope) {  //时间选择确认事件
            $scope.formData.startDate =data.startDate? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD"):null;
            $scope.formData.endDate = data.endDate?moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD"):null;

        }
    };
    $scope.submit =function(){
        $scope.formData.singerDate =$filter('date')($scope.time,"yyyy-MM-dd");
        $scope.formData.cpId= $scope.data.cpId;
        $scope.formData.partyB= $scope.data.realName;
        ContractManagementManager.contractAdd($scope.formData).then(function (result) {
            if(result.data.c=="s"){
                $state.go('main.contractManagement',{},{reload:true})
            }
        })
    };
        $scope.contractLoading=false;
        $scope.picLoading=false;

        angular.element(document.querySelector('#contractLoad')).on('change', function () {
            $scope.contractLoading = false;
        });


        $scope.contract = new FileUploader(
            {
                alias: "uploadFile",
                autoUpload:true,
                removeAfterUpload: true,//上传后删除文件 *!/
                filters: [{
                    name: 'uploadFile',//后台接收字段
                    fn: function (item, options) {
                        if (!/\.(pdf)$/.test(item.name)) {
                            $scope.imageMsg = "合约文件只能上传PDF文件";
                            $scope.imageMsg_a=true;
                            $scope.contractLoading=false;
                            return false;
                        }else{
                            $scope.imageMsg ="";
                            $scope.contractLoading=true;
                            return true;
                        }
                    }
                }],
                onSuccessItem: function (fileItem, response, headers) {
                    if (response.c == "s") {
                        $scope.formData.fileUrl=response.fileVO.webUrl;
                        $scope.contractLoading=false;
                        $scope.picLoading=true;
                    } else {
                        fileItem.isSuccess = false;
                        $scope.contractLoading=false;
                        alert("上传  " + fileItem.file.name + " 失败，请删除后重新添加上传");
                    }
                },
                url:  api+"/admin/contract/image",
                headers: {'X-Auth-Token': $scope.userInfo.token}
            }
        );

        $scope.cancel=function () {
            $scope.imageMsg_a=true;
            $state.go('main.contractManagement',{},{reload:true})
        }

    }])

